#include <vector>
#include <stack>
#include <iostream>
#include <algorithm>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode * left;
    TreeNode * right;
    TreeNode() : val(0), left(nullptr), right(nullptr)
    {}
    TreeNode(int val) : val(val), left(nullptr), right(nullptr)
    {}
    TreeNode(int val, TreeNode * left, TreeNode * right) : val(val), left(left), right(right)
    {}
};


class Solution 
{
public:
    vector<int> postorderTraversal(TreeNode* root) 
    {
        vector<int> result;
        stack<TreeNode *> st;
        if(root == nullptr)
        {
            return result;
        }
        st.push(root);
        while (!st.empty())
        {
            TreeNode * currentNode = st.top();
            result.push_back(currentNode->val);
            st.pop();
            if(currentNode->left != nullptr)
            {
                st.push(currentNode->left);
            }
            if(currentNode->right != nullptr)
            {
                st.push(currentNode->right);
            }
        }
        reverse(result.begin(), result.end());
        return result;
    }
};

// 辅助函数：创建二叉树节点
TreeNode* createNode(int val) {
    return new TreeNode(val);
}

// 辅助函数：打印遍历结果
void printResult(const vector<int>& result) {
    cout << "[";
    for (int i = 0; i < result.size(); i++) {
        cout << result[i];
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode* root) {
    if (root == nullptr) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

int main() {
    Solution solution;
    
    // 测试案例1: 示例1 - [1,null,2,3]
    //     1
    //      \
    //       2
    //      /
    //     3
    // 预期输出: [3,2,1]
    cout << "测试案例1: [1,null,2,3]" << endl;
    TreeNode* root1 = createNode(1);
    root1->right = createNode(2);
    root1->right->left = createNode(3);
    
    vector<int> result1 = solution.postorderTraversal(root1);
    cout << "输出: ";
    printResult(result1);
    cout << "预期: [3,2,1]" << endl << endl;
    
    // 测试案例2: 空树
    cout << "测试案例2: []" << endl;
    TreeNode* root2 = nullptr;
    
    vector<int> result2 = solution.postorderTraversal(root2);
    cout << "输出: ";
    printResult(result2);
    cout << "预期: []" << endl << endl;
    
    // 测试案例3: 只有一个节点
    cout << "测试案例3: [1]" << endl;
    TreeNode* root3 = createNode(1);
    
    vector<int> result3 = solution.postorderTraversal(root3);
    cout << "输出: ";
    printResult(result3);
    cout << "预期: [1]" << endl << endl;
    
    // 测试案例4: 完全二叉树
    //       1
    //      / \
    //     2   3
    //    / \
    //   4   5
    // 预期输出: [4,5,2,3,1]
    cout << "测试案例4: [1,2,3,4,5]" << endl;
    TreeNode* root4 = createNode(1);
    root4->left = createNode(2);
    root4->right = createNode(3);
    root4->left->left = createNode(4);
    root4->left->right = createNode(5);
    
    vector<int> result4 = solution.postorderTraversal(root4);
    cout << "输出: ";
    printResult(result4);
    cout << "预期: [4,5,2,3,1]" << endl << endl;
    
    // 测试案例5: 左偏树
    //   1
    //  /
    // 2
    ///
    //3
    // 预期输出: [3,2,1]
    cout << "测试案例5: 左偏树 [1,2,null,3]" << endl;
    TreeNode* root5 = createNode(1);
    root5->left = createNode(2);
    root5->left->left = createNode(3);
    
    vector<int> result5 = solution.postorderTraversal(root5);
    cout << "输出: ";
    printResult(result5);
    cout << "预期: [3,2,1]" << endl << endl;
    
    // 测试案例6: 右偏树
    // 1
    //  \
    //   2
    //    \
    //     3
    // 预期输出: [3,2,1]
    cout << "测试案例6: 右偏树 [1,null,2,null,3]" << endl;
    TreeNode* root6 = createNode(1);
    root6->right = createNode(2);
    root6->right->right = createNode(3);
    
    vector<int> result6 = solution.postorderTraversal(root6);
    cout << "输出: ";
    printResult(result6);
    cout << "预期: [3,2,1]" << endl << endl;
    
    // 清理内存
    deleteTree(root1);
    deleteTree(root3);
    deleteTree(root4);
    deleteTree(root5);
    deleteTree(root6);
    // root2 是 nullptr，不需要删除
    
    return 0;
}